import {ref} from 'vue'
import {defineStore} from 'pinia'

export const usegoodsStore = defineStore('goods', () => {
    const goodsInfo = ref([
        {
            id: 0,
            name: '可乐',//商品名称
            price: '¥ 30.00',//价格
            status: 1,//状态
            img: 'https://q5.itc.cn/images01/20240730/afe57e39449643949a8cbb53f50f1f6d.jpeg',//图片
            time: '2023-01-01'
        },
    ])
//增加
    const increment = (item) => {
        goodsInfo.value.push(item)
        console.log(goodsInfo.value, 'goodsInfo')
        sessionStorage.setItem('goodsInfo', JSON.stringify(goodsInfo.value))
    }
    //编辑
    const edit = (item) => {
        goodsInfo.value.map((v, i) => {
            if (v.id === item.id) {
                goodsInfo.value[i] = item
            }
        })
        sessionStorage.setItem('goodsInfo', JSON.stringify(goodsInfo.value))
    }
//删除
    const del = (id) => {
        goodsInfo.value.map((v, i) => {
            if (v.id === id) {
                goodsInfo.value.splice(i, 1)
            }
        })
        sessionStorage.setItem('goodsInfo', JSON.stringify(goodsInfo.value))
    }


    return {goodsInfo, increment, edit, del}
})
